define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    if(Config.domain == '' || Config.domain == null){
        Config.domain = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'virtualmachine/index/?domain=' + Config.domain,
                    add_url: 'virtualmachine/add',
                    edit_url: 'virtualmachine/edit',
                    del_url: 'virtualmachine/del',
                    multi_url: 'virtualmachine/multi',
                    import_url: 'virtualmachine/import',
                    table: 'fedition_account',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        // {field: 'ip', title: __('Ip'), operate: 'LIKE'},
                        // {field: 'ippassword', title: __('Ippassword'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'emailpassword', title: __('Emailpassword'), operate: 'LIKE'},
                        {field: 'iptime', title: __('Iptime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'payment_method', title: __('支付方式'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'自动支付': 'info','手动支付':'warning'}},
                        {field: 'admin_status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        // {
                        //     field: 'buttons',
                        //     width: "120px",
                        //     title: __('操作记录'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     operate: false,
                        //     buttons: [
                        //         {
                        //             name: 'detail',
                        //             text: __('操作记录'),
                        //             title: __('操作记录'),
                        //             classname: 'btn btn-sm btn-success btn-dialog',
                        //             icon: 'fa fa-magic',
                        //             url: 'feditionlog/index/?type=4'
                        //         }
                        //     ],
                        //     formatter: Table.api.formatter.buttons
                        // },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(document).on("click", ".btn-manual", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("virtualmachineapply/manual?name=" + name, __('手动添加账户申请'));
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var numbertype = $('#c-numbertype option:first').val();
            add_number(numbertype);
            $("#c-numbertype").change(function(){
                var numbertype = $(this).children('option:selected').val();
                add_number(numbertype);
            });
            function add_number(numbertype) {
                Fast.api.ajax({
                    url: "feditionaccount/add_number",
                    async: false,
                    data: {numbertype: numbertype}
                }, function (data) {
                    $("#c-number").val(data.number);
                    $("#c-numberid").val(data.numberid);
                    return false;
                });
            }

            $(document).on("fa.event.appendfieldlist", "#add-form .btn-append", function (e, obj) {
                Form.events.selectpage(obj);
            });
            Controller.api.bindevent();
        },
        edit: function () {
            var id = $('#c-id').val();
            $("#c-numbertype").change(function(){
                var numbertype = $(this).children('option:selected').val();
                add_number(numbertype,id);
            });
            function add_number(numbertype,id) {
                Fast.api.ajax({
                    url: "feditionaccount/add_number",
                    async: false,
                    data: {numbertype: numbertype,id: id}
                }, function (data) {
                    $("#c-number").val(data.number);
                    $("#c-numberid").val(data.numberid);
                    return false;
                });
            }

            $(document).on("fa.event.appendfieldlist", "#edit-form .btn-append", function (e, obj) {
                Form.events.selectpage(obj);
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});